package com.example.basic.window.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;

import java.util.Arrays;

public class CoustomDialog extends Dialog {

    //内容View
    private View mContentView;
    //透明背景
    private final ColorDrawable mBgTransparent = new ColorDrawable(Color.parseColor("#00000000"));
    //圆角背景
    private GradientDrawable gradientDrawable;

    public CoustomDialog(CustomDialogParams attribute, Context context) {
        super(context);
        initDialogParams(attribute,context);
    }

    public CoustomDialog(CustomDialogParams attribute, Context context, int themeResId) {
        super(context, themeResId);
        initDialogParams(attribute,context);
    }

    public CoustomDialog(CustomDialogParams attribute, Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initDialogParams(attribute,context);
    }

    private static CoustomDialog create(CustomDialogParams attribute, Context context) {
        return new CoustomDialog(attribute,context);
    }

    private static CoustomDialog create(CustomDialogParams attribute, Context context, int themeResId) {
        return new CoustomDialog(attribute,context, themeResId);
    }

    private static CoustomDialog create(CustomDialogParams attribute, Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        return new CoustomDialog(attribute,context, cancelable, cancelListener);
    }

    //初始化的操作
    private void initDialogParams(CustomDialogParams attribute, Context context) {
        //生产内容背景
        buildGradientDrawable(attribute);

        //解决Dialog无法设置宽度布满屏幕的问题
        //设置背景透明
        if (attribute.mIsBgTransparent) {
            getWindow().setBackgroundDrawable(mBgTransparent);
        }
        //设置内容
        if ((attribute.mLayoutParams != null)) {
            setContentView(attribute.mContentView, attribute.mLayoutParams);
        } else {
            setContentView(attribute.mContentView);
        }
        mContentView = attribute.mContentView;
        mContentView.setBackground(gradientDrawable);
        //设置宽高
        //窗口属性
        WindowManager.LayoutParams mWindowAttributes = getWindow().getAttributes();
        if (attribute.mWindowSize != null) {
            if (attribute.mWindowSize.x > 0) {
                mWindowAttributes.width = attribute.mWindowSize.x;
            }

            if (attribute.mWindowSize.y > 0) {
                mWindowAttributes.height = attribute.mWindowSize.y;
            }
        }

        //设置显示位置
        getWindow().setGravity(attribute.mGravity);
        //设置窗口偏移
        if (attribute.mWindowTranslate != null) {
            mWindowAttributes.x = attribute.mWindowTranslate.x;
            mWindowAttributes.y = attribute.mWindowTranslate.y;
        }
        //设置背景透明
        if (attribute.mIsBgTransparent) {
            getWindow().setBackgroundDrawable(mBgTransparent);
        }
        //设置外圈透明度
        getWindow().setDimAmount(attribute.mOutSideAlphe);
        //设置出入动画
        if (attribute.mAnimationsId != 0) {
            getWindow().setWindowAnimations(attribute.mAnimationsId);
        }
    }

    private void buildGradientDrawable(CustomDialogParams attribute) {
        //设置圆角
        gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(Color.WHITE);
        gradientDrawable.setCornerRadii(attribute.mRadius);
        gradientDrawable.setStroke(attribute.mStrokeWidth,attribute.mStrokeColor);
    }

    private View getViewById(int id) {
        return mContentView.findViewById(id);
    }

    private View getContentView() {
        return mContentView;
    }

    public static class CustomDialogParams {

        private final float[] mRadius = new float[8];
        private Point mWindowSize = null;
        private Point mWindowTranslate = null;
        private int mGravity = -1;
        private boolean mIsBgTransparent = true;
        private float mOutSideAlphe = 1.0f;
        private int mAnimationsId = 0;
        private int mStrokeColor = 0xFFFFFFFF;
        private int mStrokeWidth = 0;
        private View mContentView = null;
        private ViewGroup.LayoutParams mLayoutParams = null;

        /**
         * 设置的单位为 px ** 不是dp **
         */
        public CustomDialogParams setCorners(float radius) {
            Arrays.fill(mRadius,radius);
            return this;
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        public CustomDialogParams setCorners(float leftTopRadius, float rightTopRadius, float leftBottomRadius, float rightBottomRadius) {
            mRadius[0] = mRadius[1] = leftTopRadius;
            mRadius[2] = mRadius[3] = rightTopRadius;
            mRadius[4] = mRadius[5] = rightBottomRadius;
            mRadius[6] = mRadius[7] = leftBottomRadius;
            return this;
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        public CustomDialogParams setCorners(float leftTopRadius_x, float leftTopRadius_y,
                                             float rightTopRadius_x, float rightTopRadius_y,
                                             float leftBottomRadius_x, float leftBottomRadius_y,
                                             float rightBottomRadius_x, float rightBottomRadius_y) {
            mRadius[0] = leftTopRadius_x;
            mRadius[1] = leftTopRadius_y;
            mRadius[2] = rightTopRadius_x;
            mRadius[3] = rightTopRadius_y;
            mRadius[4] = leftBottomRadius_x;
            mRadius[5] = leftBottomRadius_y;
            mRadius[6] = rightBottomRadius_x;
            mRadius[7] = rightBottomRadius_y;
            return this;
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        public CustomDialogParams setWindowSize(int windowWidth, int windowHeight) {
            if (mWindowSize == null) {
                mWindowSize = new Point();
            }
            mWindowSize.x = windowWidth;
            mWindowSize.y = windowHeight;
            return this;
        }

        /**
         * ex: Gravity.CENTER
         */
        public CustomDialogParams setGravity(int gravity) {
            mGravity = gravity;
            return this;
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        public CustomDialogParams setWindowTranslate(int x, int y) {
            if (mWindowTranslate == null) {
                mWindowTranslate = new Point();
            }

            mWindowTranslate.x = x;
            mWindowTranslate.y = y;
            return this;
        }

        /**
         * 设置window背景是否透明
         */
        public CustomDialogParams setBgTrasparent(boolean isBgTransparent) {
            mIsBgTransparent = isBgTransparent;
            return this;
        }

        /**
         * 设置弹窗外圈透明度
         */
        public CustomDialogParams setBgOutSizeAlpha(float alpha) {
            mOutSideAlphe = alpha;
            return this;
        }

        /**
         * 设置动画
         * ex: values/style.xml
         *     <style name="DialogTransAnim">
         *         <item name="android:windowEnterAnimation">@anim/trans_enter_anim</item>
         *         <item name="android:windowExitAnimation">@anim/trans_exit_anim</item>
         *     </style>
         *  id = R.style.DialogTransAnim
         */
        public CustomDialogParams setAnimations(@StyleRes int id) {
            mAnimationsId = id;
            return this;
        }

        /**
         * 设置的单位为 px ** 不是dp **
         */
        public CustomDialogParams setStrokeColorAndWidth(int width, int color) {
            mStrokeColor = color;
            mStrokeWidth = width;
            return this;
        }

        /**
         * 设置内容
         */
        public CustomDialogParams contentView(int layoutResID, Context context) {
            mContentView = LayoutInflater.from(context).inflate(layoutResID, null, false);
            return this;
        }

        /**
         * 设置内容
         */
        public CustomDialogParams contentView(@NonNull View contentView, @Nullable ViewGroup.LayoutParams params) {
            mContentView = contentView;
            mLayoutParams = params;
            return this;
        }

        /**
         * 设置内容
         */
        public CustomDialogParams contentView(View contentView) {
            mContentView = contentView;
            return this;
        }

        public CoustomDialog build(Context context) {
            return create(this,context);
        }

        public CoustomDialog build(Context context, int themeResId) {
            return create(this,context,themeResId);
        }

        public CoustomDialog build(Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
            return create(this,context,cancelable,cancelListener);
        }
    }

}
